package us.mooresr.webtroopmanager.client.activity;

import java.util.List;

import us.mooresr.webtroopmanager.client.ClientFactory;
import us.mooresr.webtroopmanager.client.TroopManagerService;
import us.mooresr.webtroopmanager.client.TroopManagerServiceAsync;
import us.mooresr.webtroopmanager.client.events.PersonAddedEvent;
import us.mooresr.webtroopmanager.client.events.PersonAddedHandler;
import us.mooresr.webtroopmanager.client.events.PersonDeletedEvent;
import us.mooresr.webtroopmanager.client.events.PersonDeletedHandler;
import us.mooresr.webtroopmanager.client.place.PeopleListPlace;
import us.mooresr.webtroopmanager.client.ui.PeopleListView;
import us.mooresr.webtroopmanager.shared.PersonSummary;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;

public class PeopleListActivity extends MyActivity implements PeopleListView.Presenter {

	
	public PeopleListActivity(PeopleListPlace place, ClientFactory clientFactory) {
		super(place, clientFactory);
	}


	@Override
	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		super.start(panel, eventBus);
		
		PeopleListView peopleListView = clientFactory.getPeopleListView();
		peopleListView.setPresenter(this);
		panel.setWidget(peopleListView.asWidget());
		
		populateList();
		
		eventBus.addHandler(PersonAddedEvent.TYPE, new PersonAddedHandler() {

			@Override
			public void onAddPerson(PersonAddedEvent event) {
				PeopleListView peopleListView = clientFactory.getPeopleListView();
				peopleListView.addPerson(event.getPersonSummary());
				
			}
			
		});
		
		eventBus.addHandler(PersonDeletedEvent.TYPE, new PersonDeletedHandler() {

			@Override
			public void onDeletePerson(PersonDeletedEvent event) {
				PeopleListView peopleListView = clientFactory.getPeopleListView();
				peopleListView.removePerson(event.getDeletedId());
				
			}
			
		});
	}

	/**
	 * Retrieves all people from the database and uses that list to fill in the PeopleListView
	 * 
	 */
	
	private void populateList() {
		TroopManagerServiceAsync troopManagerService = (TroopManagerServiceAsync) GWT.create(TroopManagerService.class);
		
		
		AsyncCallback<List<PersonSummary>> callback = new AsyncCallback<List<PersonSummary>>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(List<PersonSummary> result) {
				
				clientFactory.getPeopleListView().setData(result);
				clientFactory.getPeopleListView().setStatus("Ready.");
			}
		};
		clientFactory.getPeopleListView().setStatus("Retrieving data...");
		troopManagerService.getAllPeople(callback);
	}
	
	@Override
	public void deletePerson(PersonSummary person) {
		Long key = person.getId();
		
		if (Window.confirm("Delete " + person.toString() + "?")) {
			TroopManagerServiceAsync troopManagerService = (TroopManagerServiceAsync) GWT.create(TroopManagerService.class);
			
			
			AsyncCallback<Long> callback = new AsyncCallback<Long>() {

				@Override
				public void onFailure(Throwable caught) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onSuccess(Long result) {
					
					clientFactory.getEventBus().fireEvent(new PersonDeletedEvent(result));
				}
			};
			
			troopManagerService.deletePerson(key, callback);
		}
		
	}

	




}
